package com.taobao.top
{
	import com.adobe.serialization.json.JSON;
	import com.taobao.top.converter.ShopCatConverter;
	import com.taobao.top.events.TOPEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	/**
	 *  前台展示的店铺类目
	 * @author 达达
	 * 
	 */	
	public class TOPShopCat extends EventDispatcher
	{
		public function TOPShopCat()
		{
		}
		/**
		 * 获取前台展示的店铺类目
		 * 
		 */		
		public function getShopCatsList():void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.shopcats.list.get")];
			topURLLdr.addEventListener(Event.COMPLETE,shopCatsGetHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		public function shopCatsGetHandler(event:Event):void
		{
			switch(event.type)
			{
				case Event.COMPLETE:
					var shopCatsArr:Array = shopCatsGetJsonParser(event.target.data);
					dispatchEvent(new TOPEvent(TOPEvent.TOP_DATA,shopCatsArr));
				break;
				default:
					 dispatchEvent(event.clone());
				break;
			}
		}
		
		private function shopCatsGetJsonParser(jsonStr:String):Array
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				var shopCatsArr:Array = new Array();
				var jsonObj:Object = JSON.decode(jsonStr).rsp.shop_cats;
				for each(var shopCatObj:Object in jsonObj)
				{
					shopCatsArr.push(ShopCatConverter.convert(shopCatObj));
				}
				return shopCatsArr;
			}
			return null;
		}
	}
}